package practice.w907;

public class PolymorphicDemo {
    public static void main(String[] args) {
        Animal[] animals = new Animal[3];
        animals[0] = new Dog("小黑",2,"黑色");
        animals[1] = new Chick("小花",1,"花色");
        animals[2] = new Fish("小金",1,"金色");
        for (int i = 0; i <animals.length ; i++) {//遍历所有动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].eat();//动物吃饭
            animals[i].drink();//每个动物喝水
            if (animals[i] instanceof Dog) {//若果对象是狗就看家
                ((Dog) animals[i]).lookHome();
            }  if (animals[i] instanceof Chick) {//如果对象是鸡就下蛋
               ((Chick) animals[i]).layEggs();
            }
            if (animals[i] instanceof Swim) {//如果对象会游泳就游泳
                  ((Swim) animals[i]).swim();
            }

        }
        Master master = new Master();
       Dog dog= new Dog("小黑",2,"黑色");
        master.feed(dog);
        Chick chick =new Chick("小花",1,"花色");
        master.feed(chick);
        Fish fish = new Fish("小金",1,"金色");
        master.feed(fish);



//        Animal[] animals = new Animal[3];
//        animals[0] = new Dog("小黑",2,"黑色");
//        animals[1] = new Chick("小花",1,"花色");
//        animals[2] = new Fish("小金",1,"金色");
//        for (int i = 0; i <animals.length ; i++) {
//            System.out.println(animals[i].name);
//            animals[i].eat();
//            animals[i].drink();
//            if (animals[i] instanceof Dog) {
//                ((Dog) animals[i]).lookHome();
//            }  if (animals[i] instanceof Chick) {
//                ((Chick) animals[i]).layEggs();
//            }
//            if (animals[i] instanceof Swim) {
//             ((Swim) animals[i]).swim();
//             }
//        }
//        Master master = new Master();
//        Dog dog= new Dog("小黑",2,"黑色");
//        master.feed(dog);
//        Chick chick =new Chick("小花",1,"花色");
//        master.feed(chick);
//        Fish fish = new Fish("小金",1,"金色");
//        master.feed(fish);
    }
}
